package com.vehicle.admin.web.Redis;

import com.google.common.collect.Lists;
import org.apache.commons.collections.map.HashedMap;

import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by yantingbin on 2017/7/12.
 */
public class Car {
    private String color;
    private Integer len;

    public Car(String color, Integer len) {
        this.color = color;
        this.len = len;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Integer getLen() {
        return len;
    }

    public void setLen(Integer len) {
        this.len = len;
    }

    @Override
    public String toString() {
        return "Car{" +
                "color='" + color + '\'' +
                ", len=" + len +
                '}';
    }

    public synchronized void method1() {
        try {
            Thread.sleep(5000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName());
    }

    public void method2() {
        System.out.println(Thread.currentThread().getName());
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException, IOException {
/*        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            int i = 1/0;
            return 100;
        });*/
//future.join();
        //future.get();
      /* String ss="asdfa";
       if(ss.equals(null)){
           System.out.println(true);
       }else{
           System.out.println(false);
       }*/

/*        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(5000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int a=10/0;
            return 100;
        });
        CompletableFuture<String> f=future.thenCompose(i->{
            return CompletableFuture.supplyAsync(()->{
               return i*10+"";
            });
        });
        System.out.println(f.get());*/
/*        final Car car = new Car("red", 100);
        Thread th1 = new Thread(() -> {
            car.method1();
        }, "thread1");
        Thread th2 = new Thread(() -> {
            car.method2();
        },"thread2");
        th1.start();
        th2.start();*/

        //List<Integer> nums = Lists.newArrayList(1,1,null,2,3,4,null,5,6,7,8,9,10);

        //System.out.println("sum is:"+nums.stream().filter(num -> num != null).distinct().mapToInt(num -> num * 2).peek(System.out::println).skip(2).limit(4).sum());

/*        List<Integer> ins=Lists.newArrayList(1,2,3,4,5,6,7,8,9,10);

        //System.out.println("ins sum is:"+ins.stream().reduce(10,(sum,item)->sum+item));
        ins.stream().max((o1,o2)->o1.compareTo(o2)).ifPresent(System.out::print);*/

        Car car = new Car("red", 10);
        Thread th1 = new Thread("thread1");
        Thread th2 = new Thread("thread2");
        th1.start();
        th2.start();

        System.in.read();

        Map<String, String> map = new HashedMap();
        for (Map.Entry<String, String> o : map.entrySet()) {

        }
    }

    public static <T> CompletableFuture<List<T>> sequence(List<CompletableFuture<T>> futures) {
        CompletableFuture<Void> allDoneFuture = CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()]));
        return allDoneFuture.thenApply(v -> futures.stream().map(CompletableFuture::join).collect(Collectors.<T>toList()));
    }

    public static <T> CompletableFuture<List<T>> sequence(Stream<CompletableFuture<T>> futures) {
        List<CompletableFuture<T>> futureList = futures.filter(f -> f != null).collect(Collectors.toList());
        return sequence(futureList);
    }

    public static synchronized void printInfo() {
        try {
            System.out.println(Thread.currentThread().getName());
            Thread.sleep(5000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
